<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form :inline="true" :model="listQuery" ref="listQuery">
      <el-form-item label="" prop="searchKey">
        <el-input
          style="width: 330px"
          clearable
          v-model="listQuery.searchKey"
          placeholder="手机号/洗车机编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="核销时间" prop="date">
        <el-date-picker
          v-model="listQuery.date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData(1)">查询</el-button>
        <el-button @click="resetSubmit">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 统计 -->
    <div class="stats">
      <span
        >核销金额：<i>{{ hxMoney }}</i
        >元</span
      >
      <span
        >成交金额：<i>{{ washCardMoney }}</i
        >元</span
      >
    </div>
    <!-- 表格 -->
    <el-table
      border
      :data="table.tableData"
      v-loading="table.loading"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="orderNo" label="订单编号" />
      <el-table-column prop="stationName" label="所属站点" />
      <el-table-column prop="mobile" label="手机号" />
      <el-table-column prop="order_No" label="核销编号" />
      <el-table-column prop="hx_StationName" label="核销结算站点" />
      <el-table-column prop="wash_Machine_Id" label="核销结算设备" />
      <el-table-column label="核销时间">
        <template slot-scope="scope">
          {{ scope.row.addtime | filterTime }}
        </template>
      </el-table-column>
      <el-table-column prop="cardInlClude" label="订单包含" />
      <el-table-column prop="hasHx" label="已核销" />
      <el-table-column prop="pendingHx" label="待核销" />
      <el-table-column prop="statusName" label="洗车状态" />
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.statusName !== '已结束'"
            type="primary"
            @click="endOrder(scope.row)"
            size="mini"
          >
            结束订单
          </el-button>
          <el-button size="mini" @click="handleInfo(scope.row)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="table.total > 0"
      :total="table.total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.pageSize"
      @pagination="getData"
    />
    <!-- 订单核销详情 -->
    <infoDialog :info="info" />
  </div>
</template>

<script>
import infoDialog from "./components/infoDialog";
export default {
  name: "OrderWriteOff",
  data() {
    return {
      // 核销金额
      hxMoney: 0,
      // 成交金额
      washCardMoney: 0,
      // 详情数据
      info: {
        data: {},
        show: false
      },
      // 查询数据
      listQuery: {
        pageSize: 10,
        pageIndex: 1,
        searchKey: "",
        date: []
      },
      // 表格数据
      table: {
        total: 0,
        tableData: [],
        loading: false
      }
    };
  },
  components: {
    infoDialog
  },
  mounted() {
    // 获取订单管理跳转携带过来的订单id
    if (this.$route.query.orderId) {
      this.listQuery.searchKey = this.$route.query.orderId;
    }
    this.getData();
  },
  methods: {
    // 打开详情
    async handleInfo(row) {
      const res = await this.$api.orderWriteOff.GetWashCarOrderDetail({
        order_No: row.order_No
      });
      this.info.data = res.data;
      if (res.data.outList.length) {
        this.info.data.outListNo = res.data.outList[0].merchantNo;
      }
      if (res.data.inputList.length) {
        this.info.data.inputListNo = res.data.inputList[0].merchantNo;
      }
      this.info.show = true;
    },
    // 重置查询
    resetSubmit() {
      this.$refs.listQuery.resetFields();
    },
    // 查询列表数据
    async getData(pageIndex) {
      if (pageIndex) {
        this.listQuery.pageIndex = pageIndex;
      }
      this.table.loading = true;
      let reqObj = {
        beginTime: this.listQuery.date && this.listQuery.date[0],
        endTime: this.listQuery.date && this.listQuery.date[1],
        pageSize: this.listQuery.pageSize,
        pageIndex: this.listQuery.pageIndex,
        searchKey: this.listQuery.searchKey
      };
      if (!reqObj.beginTime) {
        delete reqObj.beginTime;
        delete reqObj.endTime;
      }
      const res = await this.$api.orderWriteOff.GetWashCarOrderList(reqObj);
      this.table.loading = false;
      this.table.tableData = res.data.pageResponse.list;
      this.table.total = res.data.pageResponse.total;
      this.hxMoney = res.data.hxMoney;
      this.washCardMoney = res.data.washCardMoney;
    },
    //结束订单
    endOrder(row) {
      this.$msgbox({
        title: "通知",
        message: "此操作将结束用户正在进行的洗车订单不可逆，请谨慎点击",
        showCancelButton: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        beforeClose: async (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = "执行中...";
            try {
              let res = await this.$api.orderWriteOff.ForceFinishOrder({
                id: row.id
              });
              done();
              instance.confirmButtonLoading = false;
              this.$message({
                message: "恭喜你，操作成功",
                type: "success"
              });
              this.listQuery.pageIndex = 1;
              this.getData();
            } catch (error) {
              done();
              instance.confirmButtonLoading = false;
            }
          } else {
            done();
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.stats {
  margin-bottom: 10px;
  span {
    margin-right: 20px;
    i {
      font-style: normal;
      font-size: 20px;
    }
  }
}
</style>
